package org.mimizet.maven;

/*
 * Copyright 2001-2005 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.apache.commons.lang.StringUtils;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.mimizet.codegen.Mimizet;
import org.mimizet.codegen.XstreamCodegen;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Runs Mimizet code generation.
 *
 * @author <a href="mailto:chaschev@gmail.com">Andrey Chaschev</a>
 * @goal mimizet
 * @phase generate-sources
 * @requiresDependencyResolution compile
 */
public class MimizetMojo  extends AbstractMojo {
    /**
     * @parameter default-value="${project}"
     * @required
     * @readonly
     */
    private MavenProject project;

    /**
     * The output directory for the sources generated by Mimizet.
     *
     * @parameter expression="${mimizet.cacheDirectory}" default-value="mimicache"
     * @required
     */
    private File cacheDirectory;

    /**
     * @parameter expression="${mimizet.mimiXml}" default-value="mimi.xml"
     * @required
     */
    private File mimiXml;

    /**
     * The output directory for the sources generated by Mimizet.
     *
     * @parameter default-value="${project.build.directory}/generated-sources/mimizet"
     * @required
     */
    private File generatedSourcesDirectory;

    /**
     * @parameter default-value="${plugin.artifacts}"
     * @readonly
     */
    private Collection pluginArtifacts;

    private boolean initialized = false;

    XstreamCodegen codegen;

    /**
     *
     */
    public void execute() throws MojoExecutionException {
        if(!initialized){
            getLog().info("initializing codegen");
            codegen = new XstreamCodegen(
                    getLog(),
                    cacheDirectory.getAbsolutePath(),
                    generatedSourcesDirectory.getAbsolutePath());

            initialized = true;
        }

        try {
            ArrayList<Mimizet.Class> classes = Mimizet.parseJson(mimiXml).classes;
            for (Mimizet.Class aClass : classes) {
                getLog().info("processing class: " + aClass.name);
                List<String> compileSourceRoots = project.getCompileSourceRoots();
                String relativeFilePath = StringUtils.replaceChars(aClass.name, '.', '/');

                for (String compileSourceRoot : compileSourceRoots) {
                    File file = new File(compileSourceRoot + "/" + relativeFilePath + ".java");
                    if(file.exists()){
                        getLog().info("processing file: " + file);
                        codegen.saveAspect(file, aClass);
                    }
                }

            }
        } catch (Exception e) {
            throw new MojoExecutionException("Error", e);
        } finally {
        }

        if (generatedSourcesDirectory.exists()) {
            project.addCompileSourceRoot(generatedSourcesDirectory.getAbsolutePath());
        }
    }
}
